package com.app.twth.ui.returnsputatay;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityReturnsPutawayBinding;
import com.app.twth.ui.lpnphoto.lpn.LPNPhotoLPNAdapter;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.FastJsonUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class ReturnsPutatayActivity extends BaseActivity<ActivityReturnsPutawayBinding, ReturnsPutatayViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 2;
    private LPNPhotoLPNAdapter adapter;
    private List<String> tallyList = new ArrayList<>();
    String lpn;

    @Override
    protected void initListener() {
        viewModel.skuListMutableLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putString("lpn", lpn);
            bundle.putSerializable("skus", FastJsonUtil.createJsonString(data));
            AppRouter.showActivityForResult(mContext, ReturnsPutataySubmitActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.lpnListMutableLiveData.observe(this, data -> {
            if (tallyList != null) {
                tallyList.clear();
            }
            tallyList.addAll(data);
            adapter.setData(tallyList, 0);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Returns(Putaway)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_putatay:
                        type = 2;
                        break;
                    case R.id.tv_pickup:
                        type = 3;
                        break;
                }
                if (tallyList == null) {
                    tallyList = new ArrayList<>();
                }

                if (adapter != null) {
                    tallyList.clear();
                    adapter.setData(tallyList, type);
                }
                viewModel.waitingPutawayIbLpns(type);
            }
        });

        adapter = new LPNPhotoLPNAdapter(this, tallyList, position -> {
            if (tallyList == null) {
                return;
            }
            if (tallyList.size() < position) {
                return;
            }
            lpn = tallyList.get(position);
            viewModel.getIbLpnSkuList(lpn);
        });
        binding.recyclerview.setHasFixedSize(true);
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        //添加装饰类
//        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerview.setAdapter(adapter);

        viewModel.waitingPutawayIbLpns(type);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                lpn = binding.tvCode.getText().toString();
                if (Utils.isEmpty(lpn)) {
                    ToastUtil.showToast(getString(R.string.scan_inbound_lpn_lower));
                    return;
                }
                viewModel.getIbLpnSkuList(lpn);
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        bottomLayoutBinding.tvHint.setText("");
        viewModel.getIbLpnSkuList(barcodeStr);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);

                if (Utils.isEmpty(barcodeStr)) {
                    return false;
                }
                bottomLayoutBinding.tvHint.setText("");
                viewModel.getIbLpnSkuList(barcodeStr);
                return false;
            }
            return false;
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            Log.e(TAG, "onActivityResult");
            viewModel.waitingPutawayIbLpns(type);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
